<?php
/**
 * 菜单
 * */
namespace app\admin\controller;
use app\admin\controller;

class Menu extends AdminBase
{
    public function _initialize()
    {
        parent::_initialize();

        // 载入模型
        $this->menu = Model('menu');
    }

    public function index()
    {
        // 后台菜单
        $menuAdmin = $this->menu->menuAdmin(0,1,'');
        
        $this->assign('menuAdmin',$menuAdmin);

        return $this->fetch();
    }
    // 添加
    public  function add(){
        // 后台分类
        $menuAdmin = $this->menu->menuAdmin();
        $this->assign('menuAdmin',$menuAdmin);

        $confirm = langOpt(lang('confirm'));
        $this->assign('show',$confirm);

        return $this->fetch();
    }
    public function add_ok(){
        $input = input('post.');
        // 组合数据
        $data['parent_id']           = $input['parent_id'];
        $data['model']                = $input['m'];
        $data['controller']           = $input['c'];
        $data['action']                = $input['a'];
        $data['param']               = $input['p'];
        $data['title']                   = $input['title'];
        $data['type']                  = $input['type'];
        $data['is_show']             = $input['is_show'];
        $data['css']                    = $input['css'];
        $data['sort']                  = $input['sort'];
        $data['add_time']           = time();

        // 验证
        $result = $this->validate($data,'menu.add');
        if(true !== $result){
            $this->error($result);
        }

        // 数据提交
        $res = $this->menu->menuInsert($data);
        if($res ==1){
            $this->success(lang('success'));
        }else{
            $this->error(lang('error'));
        }
    }

    // 修改
    public function edit(){
        $id = input('id/d','','intval');

        $res = $this->menu->menuInfo($id);

        // 后台分类
        $menuAdmin = $this->menu->menuAdmin();
        $this->assign('menuAdmin',$menuAdmin);

        $confirm = langOpt(lang('confirm'),$res['is_show']);
        $this->assign('show',$confirm);

        $this->assign('res',$res);

        return  $this->fetch();
    }
    public function edit_ok(){
        $post = input('post.');

        // 组合数据
        $data['parent_id']             = $post['parent_id'];
        $data['title']                     = $post['title'];
        $data['model']                  = $post['m'];
        $data['controller']             = $post['c'];
        $data['action']                  = $post['a'];
        $data['param']                  = $post['p'];
        $data['css']                      = $post['css'];
        $data['sort']                     =  $post['sort'];
        $data['is_show']                = $post['is_show'];
        
        // 条件
        $where['id'] = $post['id'];

        $res = $this->menu->menuUpdate($post['id'],$data);
        //$res = db('menu')->where($where)->update($data);
        if($res ==1){
            $this->success(lang('success'));
        }else{
            $this->error(lang('error'));
        }

    }


    // 删除
    public function delete(){
        $id               = input('id/d');

        $result = $this->menu->menuDelete($id);
        if($result == 1){
            $this->success(lang('success'));
        }else{
            $this->error(lang('error'));
        }
    }

}
